home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Time.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  160 lines

  1. /*
  2.  * @(#)Time.java    1.7 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>This class is a thin wrapper around java.util.Date that allows
  19.  * JDBC to identify this as a SQL TIME value. It adds formatting and
  20.  * parsing operations to support the JDBC escape syntax for time
  21.  * values.
  22.  */
  23. public class Time extends java.util.Date {
  24.  
  25.     /**
  26.      * Construct a Time Object
  27.      *
  28.      * @param hour 0 to 23
  29.      * @param minute 0 to 59
  30.      * @param second 0 to 59
  31.      */
  32.     public Time(int hour, int minute, int second) {
  33.     super(70, 0, 1, hour, minute, second);
  34.     }
  35.    
  36.     /**
  37.      * Construct a Time using a milliseconds time value
  38.      *
  39.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  40.      */
  41.     public Time(long time) {
  42.     // If the millisecond time value contains date info, mask it out.
  43.     super(time);
  44.     int hours = getHours();
  45.     int minutes = getMinutes();
  46.     int seconds = getSeconds();
  47.     super.setTime(0);
  48.     setHours(hours);
  49.     setMinutes(minutes);
  50.     setSeconds(seconds);
  51.     }
  52.  
  53.     /**
  54.      * Set a Time using a milliseconds time value
  55.      *
  56.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT
  57.      */
  58.     public void setTime(long time) {
  59.     // If the millisecond time value contains date info, mask it out.
  60.     super.setTime(time);
  61.     int hours = getHours();
  62.     int minutes = getMinutes();
  63.     int seconds = getSeconds();
  64.     super.setTime(0);
  65.     setHours(hours);
  66.     setMinutes(minutes);
  67.     setSeconds(seconds);
  68.     }
  69.  
  70.     /**
  71.      * Convert a string in JDBC time escape format to a Time value
  72.      *
  73.      * @param s time in format "hh:mm:ss"
  74.      * @return corresponding Time
  75.      */
  76.     public static Time valueOf(String s) {
  77.     int hour;
  78.     int minute;
  79.     int second;
  80.     int firstColon;
  81.     int secondColon;
  82.  
  83.     if (s == null) throw new java.lang.IllegalArgumentException();
  84.  
  85.     firstColon = s.indexOf(':');
  86.     secondColon = s.indexOf(':', firstColon+1);
  87.     if ((firstColon > 0) & (secondColon > 0) & 
  88.         (secondColon < s.length()-1)) {
  89.         hour = Integer.parseInt(s.substring(0, firstColon));
  90.         minute = 
  91.         Integer.parseInt(s.substring(firstColon+1, secondColon));
  92.         second = Integer.parseInt(s.substring(secondColon+1));        
  93.     } else {
  94.         throw new java.lang.IllegalArgumentException();
  95.     }
  96.  
  97.     return new Time(hour, minute, second);
  98.     }
  99.    
  100.     /**
  101.      * Format a time in JDBC date escape format  
  102.      *
  103.      * @return a String in hh:mm:ss format
  104.      */
  105.     public String toString () {
  106.     int hour = super.getHours();
  107.     int minute = super.getMinutes();
  108.     int second = super.getSeconds();
  109.     String hourString;
  110.     String minuteString;
  111.     String secondString;
  112.  
  113.     if (hour < 10) {
  114.         hourString = "0" + hour;
  115.     } else {        
  116.         hourString = Integer.toString(hour);
  117.     }
  118.     if (minute < 10) {
  119.         minuteString = "0" + minute;
  120.     } else {        
  121.         minuteString = Integer.toString(minute);
  122.     }
  123.     if (second < 10) {
  124.         secondString = "0" + second;
  125.     } else {        
  126.         secondString = Integer.toString(second);
  127.     }
  128.     return (hourString + ":" + minuteString + ":" + secondString);
  129.     }
  130.  
  131.     // Override all the date operations inherited from java.util.Date;
  132.     public int getYear() {
  133.     throw new java.lang.IllegalArgumentException();
  134.     }
  135.  
  136.     public int getMonth() {
  137.     throw new java.lang.IllegalArgumentException();
  138.     }
  139.     
  140.     public int getDay() {
  141.     throw new java.lang.IllegalArgumentException();
  142.     }
  143.  
  144.     public int getDate() {
  145.     throw new java.lang.IllegalArgumentException();
  146.     }
  147.  
  148.     public void setYear(int i) {
  149.     throw new java.lang.IllegalArgumentException();
  150.     }
  151.  
  152.     public void setMonth(int i) {
  153.     throw new java.lang.IllegalArgumentException();
  154.     }
  155.  
  156.     public void setDate(int i) {
  157.     throw new java.lang.IllegalArgumentException();
  158.     }
  159. }
  160.